<template>
  <div id="container" class="container">
    <el-tabs v-model="activeName" class="tab-card tabs-white" type="card" @tab-click="handleClick">
      <el-tab-pane v-for="item in langList" :key="item.id" :label="item.lang" :name="item.lang"></el-tab-pane>
    </el-tabs>
    <div class="content">
      <div class="table-search">
        <el-button type="primary" icon="el-icon-plus" @click="goAdd">
          {{ $t('添加栏目') }}
        </el-button>
        <el-button type="primary" icon="el-icon-document-copy" @click="dialogShow = true">
          {{ $t('复制站点栏目') }}
        </el-button>
        <el-input v-model="category_title"></el-input>
        <el-button type="primary" class="search-btn" @click="handleSearch">
          {{ $t('搜索') }}
        </el-button>
        <el-button class="reset-btn" @click="handleReset">
          {{ $t('重置') }}
        </el-button>
      </div>
      <el-table ref="refTable" :data="tableData" row-key="id" :indent="30" :expand-row-keys="defaultKeys"
        @row-click="expandChange">
        <el-table-column label="ID" width="150px" prop="id" />
        <el-table-column :label="$t('栏目名称')" prop="title" />
        <el-table-column :label="$t('栏目地址')" prop="fullUrl">
          <template slot-scope="scope">
            <div @click.stop>
              <el-popover placement="top-start" title="" width="" trigger="click" :content="scope.row.fullUrl">
                <span slot="reference" class="subTitle">
                  {{ scope.row.fullUrl }}
                </span>
              </el-popover>
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('缩略图')" width="100px" prop="thumbnail">
          <template slot-scope="scope">
            <el-image v-if="scope.row.thumbnail" :src="scope.row.thumbnail.url" fit="cover"
              style="width: 32px; height: 32px" :preview-src-list="[scope.row.thumbnail.url]">
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column>
        <!-- <el-table-column :label="$t('banner图')" width="100px" prop="banner">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.banner"
              :src="scope.row.banner.url"
              fit="cover"
              style="width: 32px; height: 32px"
              :preview-src-list="[scope.row.banner.url]"
            >
              <div slot="error" class="image-slot">
                <i class="el-icon-picture-outline"></i>
              </div>
            </el-image>
          </template>
        </el-table-column> -->
        <el-table-column :label="$t('栏目排序')" prop="sort" width="120px" align="center" />
        <el-table-column :label="$t('是否启用')" prop="status" width="250px" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.status" :active-value="1" :inactive-value="2" :active-text="$t('正常')"
              :inactive-text="$t('禁用')" active-color="var(--huocms-com-color-primary)" inactive-color="rgba(0,0,0,0.2)"
              @change="disableUser(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" align="right" fixed="right" width="150px">
          <template slot-scope="scope">
            <EditBtn @click.stop="editor(scope.row)" />
            <DelBtn @success="del(scope.row)" :tips-content="$t('删除该栏目')" />
            <BaseDropdown @command="handleCommand($event, scope.row)">
              <el-dropdown-item command="add">
                {{ $t('添加子栏目') }}
              </el-dropdown-item>
              <el-dropdown-item command="view" v-if="scope.row.is_design == 1">
                {{ $t('可视化设计') }}
              </el-dropdown-item>
            </BaseDropdown>
          </template>
        </el-table-column>
      </el-table>
      <BaseDialog :title="$t('选择站点')" :dialog-show.sync="dialogShow" width="40%" class="copy" footer @submit="goCopy">
        <el-form :data="copyForm" label-position="top">
          <el-form-item :label="$t('目标站点')">
            <el-select v-model="copyForm.target_site" multiple>
              <el-option v-for="item in options" :key="item.id" :label="item.domain" :value="item.id"
                :disabled="item.id == siteId ? true : false" />
            </el-select>
          </el-form-item>
        </el-form>
      </BaseDialog>
    </div>
  </div>
</template>
<script>
import columnApi from '@/api/column.js'
import websiteApi from '@/api/website.js'
import websetApi from '@/api/websetting'
export default {
  data() {
    return {
      dialogShow: false,
      tableData: [], // 文章分类列表
      options: [], // 站点列表
      copyForm: [],
      value: '',
      website_id: '',
      searchForm: {
        website_id: ''
      },
      langList: '',
      activeName: 'zh',
      addForm: {
        title: '',
        parent_id: 0,
        website_id: '',
        alias: '',
        module_id: '',
        list_tpl: '',
        detail_tpl: '',
        type: ''
      },
      // val: {
      //   status: 2
      // },
      siteId: '',
      defaultKeys: [],
      category_title: ''
    }
  },
  computed: {
    siteId1() {
      return this.$store.getters.siteId
    }
  },
  watch: {
    siteId1: {
      handler(val) {
        if (val) {
          this.siteId = val
          this.getLang()
        }
      },
      immediate: true,
      deep: true
    }
  },
  created() {
    this.siteId = this.$store.getters.siteId
    const path = this.$route.query.key
    if (path) {
      const arr = path.split('-')
      arr.splice(0, 1)
      this.defaultKeys = arr
    }
    const lang = this.$route.query.lang
    if (lang) {
      this.activeName = lang
    }
    if (this.siteId) {
      this.getLang()
    }
    this.getWeblist()
  },

  methods: {
    handleCommand(key, row) {
      switch (key) {
        case 'add':
          this.add(row)
          break
        case 'view':
          this.viewDesign(row)
          break
      }
    },
    viewDesign(row) {
      const url =
        '/design/index?website_id=' +
        this.siteId +
        '&lang=' +
        this.activeName +
        '&html=' +
        row.design_path
      window.open(url)
    },
    // 复制站点栏目
    goCopy() {
      const val = {
        original_site: this.siteId,
        lang: this.activeName,
        target_site: this.copyForm.target_site
      }
      columnApi.copyColumn(val).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.dialogShow = false
        }
      })
    },
    // 切换栏目状态
    disableUser(row) {
      const val = {
        id: row.id,
        title: row.title,
        parent_id: row.parent_id,
        website_id: row.website_id,
        alias: row.alias,
        module_id: row.module_id,
        list_tpl: row.list_tpl,
        detail_tpl: row.detail_tpl,
        type: row.type,
        lang: row.lang,
        status: ''
      }
      if (row.status == 1) {
        val.status = 1
      } else {
        val.status = 2
      }

      columnApi.columnUpdate(val).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
        }
      })
    },
    // 获取站点语言
    getLang() {
      websetApi.langInfo({ site_id: this.siteId }).then(res => {
        this.langList = res.data
        this.activeName = this.activeName || 'zh'
        this.getList()
      })
    },
    // tab切换
    handleClick(val) {
      if (this.$route.query.lang) {
        this.$route.query.lang = val.name
      }
      this.activeName = val.name
      this.getList()
    },
    expandChange(row, index, e) {
      this.$refs.refTable.toggleRowExpansion(row)
    },

    // 获取站点列表
    getWeblist() {
      websiteApi.websiteList().then(res => {
        this.options = res.data
        // this.siteId = res.data[0].id
        // this.getLang()
      })
    },
    // 删除
    del(row) {
      const val = {
        website_id: this.siteId,
        id: row.id,
        lang: this.activeName
      }
      columnApi.columnDelete(val).then(res => {
        if (res.code == 0) {
          this.$message.success(res.msg)
          this.getList()
        }
      })
    },
    // 新增一级
    goAdd() {
      this.$router.push({
        name: 'addColumn',
        query: { pid: 0, website_id: this.siteId, lang: this.activeName }
      })
    },
    // 新增子级
    add(row) {
      this.$router.push({
        name: 'addColumn',
        query: {
          path: row.path,
          id: row.id,
          website_id: this.siteId,
          lang: this.activeName,
          module_id: row.module_id
        }
      })
    },
    // 编辑
    editor(row) {
      this.$router.push({
        name: 'updateColumn',
        query: { id: row.id, website_id: row.website_id, lang: row.lang }
      })
    },
    // 提交
    submit() {
      this.$refs.addForm.validate(valid => {
        if (valid) {
          if (!this.addForm.id) {
            const val = {
              title: this.addForm.title,
              parent_id: this.addForm.parent_id,
              website_id: this.website_id,
              alias: this.addForm.alias,
              module_id: this.addForm.module_id,
              list_tpl: this.addForm.list_tpl,
              type: this.addForm.type,
              detail_tpl: this.addForm.detail_tpl
            }

            columnApi.columnSave(this.addForm).then(res => {
              if (res.code == 0) {
                this.$message.success(res.msg)
                this.getList()
              }
            })
          } else {
            // const val = {
            //   id: this.addForm.id,
            //   name: this.addForm.name,
            //   pid: this.addForm.pid,
            //   status: this.addForm.status
            // }
            columnApi.columnUpdate(this.addForm).then(res => {
              if (res.code == 0) {
                this.$message.success(res.msg)
                this.getList()
              }
            })
          }
        } else {
          this.$message.error(this.$t('请填写必要信息'))
        }
      })
    },
    // 栏目列表
    getList() {
      // this.website_id = this.siteId
      const val = {
        website_id: this.siteId,
        lang: this.activeName,
        category_title: this.category_title
      }
      columnApi.columnList(val).then(res => {
        this.tableData = res.data
      })
    },
    handleSearch() {
      this.getList()
    },
    handleReset() {
      this.category_title = ''
      this.getList()
    }
  }
}
</script>
<style lang="scss">
.copy {
  .el-select {
    width: 100%;
  }
}

#container .columnDialog {
  .el-form--inline .el-form-item__content {
    width: 410px;
  }

  .el-select {
    width: 100%;
  }
}
</style>
<style scoped lang="scss">
.table-search {
  display: flex;
  align-items: center;
  margin-bottom: 16px;

  .el-input {
    width: 300px;
    margin-left: 20px;
  }

  .search-btn,
  .reset-btn {
    margin-left: 10px;
  }
}

::v-deep {
  .tab-card.el-tabs--card .el-tabs__content {
    padding: 0 16px;
  }
}

.sort_icon {
  height: 30px;
  vertical-align: middle;
  border-radius: 5px;
}

.content {
  background-color: #fff;
  padding: 16px 16px;
}

.subTitle {
  cursor: pointer;
  overflow: hidden;
  display: -webkit-box;
  text-overflow: ellipsis;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  white-space: normal;
}
</style>
